/*
    Copyright (C) 2008 Edwin Lee

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function onOpen()
{
	var editFlickrUserName = view.children.item("editFlickrUserNameDiv").children.item("editFlickrUserName");
	
	if (options.exists(KEY_FLICKR_USER_NAME))
	{
		editFlickrUserName.value = options.getValue(KEY_FLICKR_USER_NAME);
	}
	
	var radSizeThumbnail = view.children.item("radSizeDiv").children.item("radSizeThumbnail");
	var radSizeSmall = view.children.item("radSizeDiv").children.item("radSizeSmall");
	var radSizeMedium = view.children.item("radSizeDiv").children.item("radSizeMedium");
	
	if (options.exists(KEY_SIZE))
	{
		var size = options.getValue(KEY_SIZE);
		
		if (size == VALUE_SIZE_THUMBNAIL)
		{
			radSizeThumbnail.value = true;
		}
		else if (size == VALUE_SIZE_SMALL)
		{
			radSizeSmall.value = true;
		}
		else if (size == VALUE_SIZE_MEDIUM)
		{
			radSizeMedium.value = true;
		}
	}
	
	var radPhotoAlignmentTopLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopLeft");
	var radPhotoAlignmentTopCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopCenter");
	var radPhotoAlignmentTopRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopRight");
	var radPhotoAlignmentMiddleLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleLeft");
	var radPhotoAlignmentMiddleCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleCenter");
	var radPhotoAlignmentMiddleRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleRight");
	var radPhotoAlignmentBottomLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomLeft");
	var radPhotoAlignmentBottomCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomCenter");
	var radPhotoAlignmentBottomRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomRight");
	
	if ((options.exists(KEY_HALIGN)) && (options.exists(KEY_VALIGN)))
	{
		var hAlign = options.getValue(KEY_HALIGN);
		var vAlign = options.getValue(KEY_VALIGN);
		
		if (hAlign == VALUE_HALIGN_LEFT)
		{
			if (vAlign == VALUE_VALIGN_TOP)
			{
				radPhotoAlignmentTopLeft.value = true;
			}
			else if (vAlign == VALUE_VALIGN_MIDDLE)
			{
				radPhotoAlignmentMiddleLeft.value = true;
			}
			else if (vAlign == VALUE_VALIGN_BOTTOM)
			{
				radPhotoAlignmentBottomLeft.value = true;
			}
		}
		else if (hAlign == VALUE_HALIGN_CENTER)
		{
			if (vAlign == VALUE_VALIGN_TOP)
			{
				radPhotoAlignmentTopCenter.value = true;
			}
			else if (vAlign == VALUE_VALIGN_MIDDLE)
			{
				radPhotoAlignmentMiddleCenter.value = true;
			}
			else if (vAlign == VALUE_VALIGN_BOTTOM)
			{
				radPhotoAlignmentBottomCenter.value = true;
			}
		}
		else if (hAlign == VALUE_HALIGN_RIGHT)
		{
			if (vAlign == VALUE_VALIGN_TOP)
			{
				radPhotoAlignmentTopRight.value = true;
			}
			else if (vAlign == VALUE_VALIGN_MIDDLE)
			{
				radPhotoAlignmentMiddleRight.value = true;
			}
			else if (vAlign == VALUE_VALIGN_BOTTOM)
			{
				radPhotoAlignmentBottomRight.value = true;
			}
		}
	}
	
	var editRefreshInterval = view.children.item("editRefreshIntervalDiv").children.item("editRefreshInterval");
	
	if (options.exists(KEY_REFRESH_INTERVAL_SEC))
	{
		editRefreshInterval.value = options.getValue(KEY_REFRESH_INTERVAL_SEC);
	}
	
	var chkFadeTransition = view.children.item("chkFadeTransition");
	
	if (options.exists(KEY_FADE_TRANSITION))
	{
		chkFadeTransition.value = options.getValue(KEY_FADE_TRANSITION);
	}
}

function onOk()
{
	options.removeAll();
	
	var editFlickrUserName = view.children.item("editFlickrUserNameDiv").children.item("editFlickrUserName");
	
	options.putValue(KEY_FLICKR_USER_NAME, editFlickrUserName.value);
	
	var radSizeThumbnail = view.children.item("radSizeDiv").children.item("radSizeThumbnail");
	var radSizeSmall = view.children.item("radSizeDiv").children.item("radSizeSmall");
	var radSizeMedium = view.children.item("radSizeDiv").children.item("radSizeMedium");
	
	if (radSizeThumbnail.value)
	{
		options.putValue(KEY_SIZE, VALUE_SIZE_THUMBNAIL);
	}
	else if (radSizeSmall.value)
	{
		options.putValue(KEY_SIZE, VALUE_SIZE_SMALL);
	}
	else if (radSizeMedium.value)
	{
		options.putValue(KEY_SIZE, VALUE_SIZE_MEDIUM);
	}
	
	var radPhotoAlignmentTopLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopLeft");
	var radPhotoAlignmentTopCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopCenter");
	var radPhotoAlignmentTopRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentTopRight");
	var radPhotoAlignmentMiddleLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleLeft");
	var radPhotoAlignmentMiddleCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleCenter");
	var radPhotoAlignmentMiddleRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentMiddleRight");
	var radPhotoAlignmentBottomLeft = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomLeft");
	var radPhotoAlignmentBottomCenter = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomCenter");
	var radPhotoAlignmentBottomRight = view.children.item("radPhotoAlignmentDiv").children.item("radPhotoAlignmentInnerDiv").children.item("radPhotoAlignmentBottomRight");
	
	if (radPhotoAlignmentTopLeft.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_LEFT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_TOP);
	}
	else if (radPhotoAlignmentTopCenter.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_CENTER);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_TOP);
	}
	else if (radPhotoAlignmentTopRight.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_RIGHT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_TOP);
	}
	else if (radPhotoAlignmentMiddleLeft.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_LEFT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_MIDDLE);
	}
	else if (radPhotoAlignmentMiddleCenter.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_CENTER);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_MIDDLE);
	}
	else if (radPhotoAlignmentMiddleRight.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_RIGHT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_MIDDLE);
	}
	else if (radPhotoAlignmentBottomLeft.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_LEFT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_BOTTOM);
	}
	else if (radPhotoAlignmentBottomCenter.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_CENTER);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_BOTTOM);
	}
	else if (radPhotoAlignmentBottomRight.value)
	{
		options.putValue(KEY_HALIGN, VALUE_HALIGN_RIGHT);
		options.putValue(KEY_VALIGN, VALUE_VALIGN_BOTTOM);
	}
	
	var editRefreshInterval = view.children.item("editRefreshIntervalDiv").children.item("editRefreshInterval");
	
	var refreshIntervalSec = parseInt(editRefreshInterval.value, 10);
	
	if (refreshIntervalSec <= 0)
	{
		refreshIntervalSec = 60;
	}
	else if (refreshIntervalSec < 10)
	{
		refreshIntervalSec = 10;
	}
	else if (refreshIntervalSec > 3600)
	{
		refreshIntervalSec = 3600;
	}
	
	options.putValue(KEY_REFRESH_INTERVAL_SEC, refreshIntervalSec);
	
	var chkFadeTransition = view.children.item("chkFadeTransition");
	
	options.putValue(KEY_FADE_TRANSITION, chkFadeTransition.value);
	
	options.putValue(KEY_OPTIONS_CHANGED, 1);
}

